package work;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Window extends JFrame implements ActionListener{
    int x,y,w,z ;
    int width =800 , height = 600;
    int speed = 100;
    JButton head = new JButton();//创建按钮
    JButton food = new JButton();
    JButton body[] = new JButton[500];
    String a = null;
    int keyCount=0;
    int bodyCount=0;
    GameRule rule = new GameRule(this);
    JLabel label;
    Font font;
    JPanel panel;
    JMenuBar menubar;
    JMenu menu1,menu2;
    JMenuItem item1,item2,item3,item4,item5;
    Timer timer;

    //构造窗体
    public Window() {
        menubar=new JMenuBar();
        menu1=new JMenu("游戏");
        menu2=new JMenu("难度");
        item1=new JMenuItem("小学生难度");
        item2=new JMenuItem("中等难度");
        item3=new JMenuItem("高手难度");
        item4=new JMenuItem("老司机难度");
        item5=new JMenuItem("开始新游戏");
        item1.addActionListener(this);          //添加事件
        item2.addActionListener(this);
        item3.addActionListener(this);
        item4.addActionListener(this);
        item5.addActionListener(this);
        menu1.add(item5);
        menu2.add(item1);
        menu2.add(item2);
        menu2.add(item3);
        menu2.add(item4);
        menubar.add(menu1);
        menubar.add(menu2);
        setJMenuBar(menubar);
        setLayout(null);      //布局    绝对布局    所有的组件都用绝对的坐标表示
        setBounds(300,150,width,height);
        setVisible(true);      //设置是否可见
        setTitle("贪吃蛇");
        label=new JLabel("分数："+0);
        label.setBounds(25,50,110,30);
        font=new Font("jk",Font.BOLD,25);
        label.setFont(font);
        panel=new JPanel();
        panel.setLayout(null);   //布局  panel容器
        add(panel);
        panel.add(label);
        panel.setBackground(Color.pink);
        panel.setBounds(650,0,150,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //设置关闭
        timer = new Timer(speed,this);
        addHead();
        addFood();
    }

    //添加食物
    public void addFood(){
        w=(int)(Math.random()*450/10)*10;
        z=(int)(Math.random()*450/10)*10;
        food.setBackground(Color.yellow);
        food.setBounds(w,z,10,10);
        add(food);
    }

    //添加头部
    public void addHead(){
        x=(int)(Math.random()*450/10)*10;
        y=(int)(Math.random()*450/10)*10;
        head.setBounds(x, y, 10, 10);//设置按钮大小，以及随机生成按钮位置
        head.setBackground(Color.red);
        add(head);//添加按钮到窗口中

        //添加键盘监控器
        head.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                keyCount++;
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        if(!"down".equals(a))
                            a = "up";
                        break;
                    case KeyEvent.VK_DOWN:
                        if(!"up".equals(a))
                            a = "down";
                        break;
                    case KeyEvent.VK_RIGHT:
                        if(!"left".equals(a))
                            a = "right";
                        break;
                    case KeyEvent.VK_LEFT:
                        if(!"right".equals(a))
                            a = "left";
                        break;
                }
                if(keyCount==1)
                    timer.start();
            }
        });
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==timer){
            if ("up".equals(a)) {
                y -= 10;
                head.setBounds(x, y, 10, 10);
                repaint();
            }
            else if ("down".equals(a)) {
                y += 10;
                head.setBounds(x, y, 10, 10);
                repaint();
            }
            else if ("left".equals(a)) {
                x -= 10;
                head.setBounds(x, y, 10, 10);
                repaint();
            }
            else if ("right".equals(a)) {
                x += 10;
                head.setBounds(x, y, 10, 10);
                repaint();
            }
            rule.setLocation(x, y);
            rule.judge(x,y,w,z);
            rule.moveBody();
        }
        else if(e.getSource()==item1){
            speed=200;
            timer=new Timer(speed,this);
            timer.start();
        }
        else if(e.getSource()==item2){
            speed=100;
            timer=new Timer(speed,this);
            timer.start();
        }
        else if(e.getSource()==item3){
            speed=50;
            timer=new Timer(speed,this);
            timer.start();
        }
        else if(e.getSource()==item4){
            speed=25;
            timer=new Timer(speed,this);
            timer.start();
        }
        else if(e.getSource()==item5){
            rule.newGame();
        }
    }
}

